from math import radians
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from random import randint
from time import sleep

redact = webdriver.Firefox()
redact.implicitly_wait(20)

# redact.get('https://adviser.raysgo.com/login')  # 测试环境
redact.get('https://adviser.raysyun.com/login')  # UAT环境

redact.find_element(
    By.CSS_SELECTOR, '[placeholder="请输入手机号"]').send_keys('13979029153')
redact.find_element(
    By.CSS_SELECTOR, '[placeholder="请输入密码"]').send_keys('Shishi1021.')


'''
redact.find_element(
    By.CSS_SELECTOR, '[placeholder="请输入手机号"]').send_keys('阿洛呀')
redact.find_element(
    By.CSS_SELECTOR, '[placeholder="请输入密码"]').send_keys('Shishi1021.')
'''
redact.find_element(By.CLASS_NAME, 'login-btn').click()


book = redact.find_element(By.ID, 'BOOK')
ActionChains(redact).move_to_element(book).perform()
book.click()

# 新手引导
try:
    redact.find_element(By.CSS_SELECTOR, 'div[class*="primary"]').click()
    redact.find_element(By.CSS_SELECTOR, 'div[class*="primary"]').click()
except:
    NoSuchElementException: print("没有引导啦")

# 新增纸质书
redact.find_element(By.CSS_SELECTOR, 'a>i+span').click()

redact.find_element(By.CSS_SELECTOR, '[aria-label="图标: down"]').click()

redact.find_element(By.CSS_SELECTOR, '[title="社科"]').click()
redact.find_element(By.CSS_SELECTOR, '[title="法律"]').click()
redact.find_element(By.CSS_SELECTOR, '[title="法律工具书"]').click()


redact.find_element(By.CSS_SELECTOR, '[title="请选择专业"]').click()
redact.find_element(By.CSS_SELECTOR, '[label="语文"]').click()

redact.find_element(By.CSS_SELECTOR, '[title="请选择深度"]').click()
redact.find_element(By.CSS_SELECTOR, '[label="一年级"]').click()

redact.find_element(By.CSS_SELECTOR, '[title="请选择目的"]').click()
redact.find_element(By.CSS_SELECTOR, '[label="健身"]').click()

redact.find_element(By.CSS_SELECTOR, '[title="请选择地域"]').click()
redact.find_element(By.CSS_SELECTOR, '[label="江西省"]').click()

redact.find_element(
    By.CSS_SELECTOR, 'label>input[value="OTHER"]+span+span').click()


# 生成一个随机整数
book_bar_code = randint(1, 999999)
number = randint(1, 999999)
title = randint(1, 9999)


redact.find_element(
    By.NAME, 'isbn').send_keys('{0}'.format(book_bar_code))
redact.find_element(By.ID, 'm_add_rays_book_5').click()
redact.find_element(
    By.NAME, 'serialNumber').send_keys('{0}'.format(number))
redact.find_element(By.NAME, 'bookName').send_keys('脚本创建{0}'.format(title))
redact.find_element(By.NAME, 'author').send_keys('阿瑾')
redact.find_element(By.NAME, 'publish').send_keys('脚本出版社')
sleep(3)    # 创建图书需要等待的时间
redact.find_element(By.CSS_SELECTOR, '[type="button"]').click()


# 新手引导
try:
    redact.find_element(By.CSS_SELECTOR, '.guide-footer>div+div').click()
except:
    InvalidSelectorException: print("没有引导啦")
sleep(5)

'''
redact.find_element(
    By.CSS_SELECTOR, 'div[class*="ApplicationProductTool"] i').click()
redact.find_element(
    By.CLASS_NAME, 'div[class*="flex-center"]>button:nth-child(1)').click()
'''

redact.find_element(By.CSS_SELECTOR, '.con>div:nth-child(2)>div i').click()

# 添加应用=5s
redact.find_element(By.CSS_SELECTOR, '.select-wrap').click()
# sleep(5)


# 选择作品
redact.find_element(By.CSS_SELECTOR, '.pointer>td:nth-child(1)').click()
redact.find_element(By.CSS_SELECTOR, '.btn-wrap>:nth-child(1)').click()
redact.find_element(By.CSS_SELECTOR, 'div[class*="audit-btn mr10"]').click()


# 删除图书步骤
mouse = ActionChains(redact)
book_001 = redact.find_element(
    By.CSS_SELECTOR, '.BlockPage>ul>div:nth-child(1)')

mouse.move_to_element(book_001).perform()
redact.find_element(
    By.CSS_SELECTOR, '.BlockPage>ul>div:nth-child(1) i').click()


redact.find_element(
    By.XPATH, '/html/body/div[1]/div/div[1]/div/div[2]/main/div/div/div[1]/div/div/div/div/div[3]/div[2]/footer/div/button[1]').click()

# time.sleep(10)
# redact.quit()
